package com.example.myapplicationtest;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

public class Appmain extends AppCompatActivity  {
String path="https://www.wanandroid.com/article/list/0/json";
    private List<Article> articleList=new ArrayList<>();
    ListView listView;
    MyAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_appmain);
        adapter=new MyAdapter();
        listView=findViewById(R.id.item_list);
        listView.setAdapter(adapter);

        new Thread(){
            @Override
            public void run() {
                String jsonString = getJsonString(path);
                articleList=parseJson(jsonString);
                runOnUiThread(new Runnable() {
                   @Override
                   public void run() {
                       adapter.notifyDataSetChanged();
                   }
               });
            }
        }.start();
        listView.setOnItemClickListener(new AdapterView . OnItemClickListener() {
                                            @Override
                                            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                                             Intent intent =new Intent(Appmain.this,ReadActivity.class);
                                             intent.putExtra("link",articleList.get(position).getLink());
                                             startActivity(intent);
                                            }
                                        }
            );


        }
    private List<Article> parseJson(String strJson){
        List<Article> articles=new ArrayList<>();
        try {
            JSONObject object=new JSONObject(strJson);
            int errorCode = object.optInt("errorCode");
            if(errorCode!=0) return null;
            JSONObject object1 = object.optJSONObject("data");
            JSONArray datas = object1.optJSONArray("datas");
            for(int i=0;i<datas.length();i++){
                JSONObject object2 = datas.optJSONObject(i);
                String title = object2.optString("title");
                String author = object2.optString("author");
                String link = object2.optString("link");
                Article article=new Article(title,author,link);
                articles.add(article);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return articles;
    }





    private String getJsonString(String path){
        try {
            URL url=new URL(path);
            HttpURLConnection cn= (HttpURLConnection) url.openConnection();
            InputStream inputStream = cn.getInputStream();
            BufferedReader br=new BufferedReader(new InputStreamReader(inputStream));
            StringBuffer buffer=new StringBuffer();
            String line=null;
            while ((line=br.readLine())!=null){
                buffer.append(line);
            }
            return buffer.toString();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
    class MyAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return articleList.size();
        }
        @Override
        public Object getItem(int position) {
            return position;
        }
        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder, holder1;
            if (convertView == null) {
                convertView = LayoutInflater.from(Appmain.this)
                        .inflate(R.layout.list_item, parent, false);

             holder = new ViewHolder();
             holder.tv_title = convertView.findViewById(R.id.tv_title);
             holder.tv_author = convertView.findViewById(R.id.tv_author);
               convertView.setTag(holder);

            } else {
               holder = (ViewHolder) convertView.getTag();
          }
            holder.tv_title.setText("标题： "+articleList.get(position).getTitle());
           holder.tv_author.setText("作者： "+articleList.get(position).getAuthor());
            return convertView;
        }
    }
   static class ViewHolder {
        TextView tv_title;
       TextView tv_author;
    }

}